import { roleMenuTreeselect } from "@/api/service/system/menu";
import { delRole, getRole, listRole } from "@/api/service/user/roles";
import { form, resetForm, showForm } from "./form";
import { keys, query, table } from "./table";

export const list = async () => {
  table.value.loading = true;
  const data = await listRole(query.value);
  table.value.data = data.data;
  query.value.total = data.data.length;
  table.value.loading = false;
};

export const edit = async (id?: string) => {
  const targetId = id || keys.value[0];
  const result = await getRole(targetId);
  const { data } = await roleMenuTreeselect(targetId);

  form.value = result.data;
  form.value.menuIds = data.checkedKeys;
  showForm.value = true;
};
export const create = async () => {
  resetForm();
  showForm.value = true;
};
export const del = async (id?: string) => {
  const targetId = [id!] || keys.value;
  await delRole(targetId);
  await list();
  keys.value = [];
};
